package com.rr.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.rr.springcloud.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author RR
 * @date 2021/3/27 20:44
 */
@RestController
public class PaymentController {
    @Autowired
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentInfoOK(@PathVariable("id") Integer id) {
        return paymentService.paymentInfoOK(id);
    }

    @HystrixCommand(fallbackMethod = "paymentInfoTimeoutFallBack",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")
    })
    @GetMapping("/payment/hystrix/timeout/{id}")
    public String paymentInfoTimeout(@PathVariable("id") Integer id) {
        return paymentService.paymentInfoTimeout(id);
    }
    public String paymentInfoTimeoutFallBack(@PathVariable("id") Integer id) {
        return "服务端2秒内未响应或出错"+"线程池：" + Thread.currentThread().getName() + ",id:" + id;
    }

    @GetMapping("/payment/hystrix/CircuitBreaker/{id}")
    public String paymentCircuitBreaker(@PathVariable("id")Integer id) {
        return paymentService.paymentCircuitBreaker(id);
    }

}
